package com.idealighter.game.web.common.controller;

import com.google.inject.Singleton;

import com.idealighter.game.core.common.Game;
import com.idealighter.game.core.common.GameIdConstant;
import com.idealighter.game.core.common.GameMap;
import com.idealighter.game.core.constant.room.RoomActiveConstant;
import com.idealighter.game.core.result.Result;
import com.idealighter.game.dictionary.dic.JcbyRoomDic;
import com.idealighter.game.dictionary.domain.BaccaratRoomDomain;
import com.idealighter.game.dictionary.domain.BairenniuniuRoomDomain;
import com.idealighter.game.dictionary.domain.BlackjackRoomDomain;
import com.idealighter.game.dictionary.domain.FruitslotRoomDomain;
import com.idealighter.game.dictionary.domain.JcbyRoomDomain;
import com.idealighter.game.dictionary.domain.LkpyRoomDomain;
import com.idealighter.game.dictionary.domain.SharkRoomDomain;
import com.idealighter.game.dictionary.domain.ShuihuRoomDomain;
import com.idealighter.game.dictionary.domwrapper.ZhajinhuaRoomDomWrapper;
import com.idealighter.game.games.baccarat.manager.BaccaratDataMgr;
import com.idealighter.game.games.baccarat.struct.BaccaratRoom;
import com.idealighter.game.games.bairenniuniu.manager.BaiRenNiuNiuDataMgr;
import com.idealighter.game.games.bairenniuniu.struct.BaiRenNiuNiuRoom;
import com.idealighter.game.games.blackjack.manager.BlackjackDataMgr;
import com.idealighter.game.games.blackjack.struct.BlackjackRoom;
import com.idealighter.game.games.fqzs.manager.SharkDataMgr;
import com.idealighter.game.games.fqzs.struct.SharkRoom;
import com.idealighter.game.games.fruitslot.manager.FruitslotDataMgr;
import com.idealighter.game.games.fruitslot.struct.FruitslotDifficulty;
import com.idealighter.game.games.fruitslot.struct.FruitslotRoom;
import com.idealighter.game.games.jcby.manager.JcbyDataMgr;
import com.idealighter.game.games.jcby.struct.JcbyDifficulty;
import com.idealighter.game.games.jcby.struct.JcbyRoom;
import com.idealighter.game.games.lkby.manager.LkbyDataMgr;
import com.idealighter.game.games.lkby.struct.LkbyDifficulty;
import com.idealighter.game.games.lkby.struct.LkbyRoom;
import com.idealighter.game.games.shuihu.manager.ShuiHuDataMgr;
import com.idealighter.game.games.shuihu.struct.ShuiHuDifficulty;
import com.idealighter.game.games.shuihu.struct.ShuiHuRoom;
import com.idealighter.game.games.sirenniuniu.manager.SiRenNiuNiuDataMgr;
import com.idealighter.game.games.sirenniuniu.struct.SiRenNiuNiuRoom;
import com.idealighter.game.games.zjh.manager.ZjhDataMgr;
import com.idealighter.game.games.zjh.struct.ZjhRoom;
import com.idealighter.game.server.context.ApplicationContext;
import com.idealighter.game.web.common.controller.convert.GameListDtoConvert;
import com.idealighter.game.web.common.controller.convert.RoomListDtoConvert;
import com.idealighter.game.web.common.controller.dto.AllRoomListDto;
import com.idealighter.game.web.common.controller.dto.FruitSlotControlDto;
import com.idealighter.game.web.common.controller.dto.GameListDto;
import com.idealighter.game.web.common.controller.dto.JcbyControlDto;
import com.idealighter.game.web.common.controller.dto.LkbyControlDto;
import com.idealighter.game.web.common.controller.dto.RoomListDto;
import com.idealighter.game.web.common.controller.dto.ShuiHuControlDto;

import java.util.ArrayList;
import java.util.List;

import javax.validation.constraints.NotNull;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/common")
@Singleton
@Produces({ MediaType.APPLICATION_JSON })
public class CommonController {
  /**
   * 获取游戏列表.
   * 
   * @return 结果.
   */
  @Path("/findGameList")
  @POST
  public Result findGameList() {

    List<Game> games = GameMap.allGame();
    List<GameListDto> list = GameListDtoConvert.INSTANCE.toGameListDto(games);

    Result result = new Result(true);
    result.getMap().put("list", list);
    return result;
  }

  /**
   * 获取游戏中的房间列表.
   * 
   * @Title findRoomListByGameId.
   * @author houdongsheng
   * @date 2018年1月27日 下午5:39:51
   * @param gameId 游戏编号
   * @return Result
   */
  @Path("/findRoomListByGameId")
  @POST
  public Result findRoomListByGameId(@NotNull @FormParam("gameId") Integer gameId) {
    List<RoomListDto> rooms = new ArrayList<RoomListDto>();
    switch (gameId) {
      case GameIdConstant.SHUI_HU_ZHUAN:
        ShuiHuDataMgr shuiHuDataMgr = ApplicationContext.getBean(ShuiHuDataMgr.class);
        for (ShuiHuRoom item : shuiHuDataMgr.newestRooms()) {
          ShuihuRoomDomain roomDomain = item.getRoomDomain();
          if (roomDomain.getIsActive() == RoomActiveConstant.ACTIVE) {
            rooms.add(RoomListDtoConvert.INSTANCE.toRoomListDto(item));
          }
        }
        break;
      case GameIdConstant.FRUIT_SLOT:
        FruitslotDataMgr fruitslotDataMgr = ApplicationContext.getBean(FruitslotDataMgr.class);
        for (FruitslotRoom item : fruitslotDataMgr.newestRooms()) {
          FruitslotRoomDomain roomDomain = item.getRoomDomain();
          if (roomDomain.getIsActive() == RoomActiveConstant.ACTIVE) {
            rooms.add(RoomListDtoConvert.INSTANCE.toRoomListDto(item));
          }
        }
        break;
      case GameIdConstant.FQZS:
        SharkDataMgr sharkDataMgr = ApplicationContext.getBean(SharkDataMgr.class);
        for (SharkRoom item : sharkDataMgr.newestRooms()) {
          SharkRoomDomain roomDomain = item.getRoomDomain();
          if (roomDomain.getIsActive() == RoomActiveConstant.ACTIVE) {
            rooms.add(RoomListDtoConvert.INSTANCE.toRoomListDto(item));
          }
        }
        break;
      case GameIdConstant.LKBY:
        LkbyDataMgr lkbyDataMgr = ApplicationContext.getBean(LkbyDataMgr.class);
        for (LkbyRoom item : lkbyDataMgr.newestRooms()) {
          LkpyRoomDomain roomDomain = item.getRoomDomain();
          if (roomDomain.getIsActive() == RoomActiveConstant.ACTIVE) {
            rooms.add(RoomListDtoConvert.INSTANCE.toRoomListDto(item));
          }
        }
        break;
      case GameIdConstant.JCBY:
        JcbyDataMgr jcbyDataMgr = ApplicationContext.getBean(JcbyDataMgr.class);
        for (JcbyRoom item : jcbyDataMgr.allRooms()) {
          JcbyRoomDomain domain = item.getRoomDomain();
          if (domain.getIsActive() == RoomActiveConstant.ACTIVE) {
            rooms.add(RoomListDtoConvert.INSTANCE.toRoomListDto(item));
          }
        }
        break;
      case GameIdConstant.SIREN_NIUNIU:
        SiRenNiuNiuDataMgr siRenNiuNiuDataMgr =
            ApplicationContext.getBean(SiRenNiuNiuDataMgr.class);
        for (SiRenNiuNiuRoom item : siRenNiuNiuDataMgr.newestRooms()) {
          if (item.getRoomDomain().getIsActive() == RoomActiveConstant.ACTIVE) {
            rooms.add(RoomListDtoConvert.INSTANCE.toRoomListDto(item));
          }
        }
        break;
      case GameIdConstant.ZJH:
        ZjhDataMgr zjhDataMgr = ApplicationContext.getBean(ZjhDataMgr.class);
        for (ZjhRoom item : zjhDataMgr.newestRooms()) {
          ZhajinhuaRoomDomWrapper wrapper = item.getRoomDomain();
          if (wrapper.getIsActive() == RoomActiveConstant.ACTIVE) {
            rooms.add(RoomListDtoConvert.INSTANCE.toRoomListDto(item));
          }
        }
        break;
      case GameIdConstant.BLACK_JACK:
        BlackjackDataMgr blkjDataMgr = ApplicationContext.getBean(BlackjackDataMgr.class);
        for (BlackjackRoom item : blkjDataMgr.newestRooms()) {
          BlackjackRoomDomain domain = item.getRoomDomain();
          if (domain.getIsActive() == RoomActiveConstant.ACTIVE) {
            rooms.add(RoomListDtoConvert.INSTANCE.toRoomListDto(item));
          }
        }
        break;

      case GameIdConstant.BACCARAT:
        BaccaratDataMgr baccaratDataMgr = ApplicationContext.getBean(BaccaratDataMgr.class);
        for (BaccaratRoom item : baccaratDataMgr.newestRooms()) {
          BaccaratRoomDomain domain = item.getRoomDomain();
          if (domain.getIsActive() == RoomActiveConstant.ACTIVE) {
            rooms.add(RoomListDtoConvert.INSTANCE.toRoomListDto(item));
          }
        }
        break;

      case GameIdConstant.BAIREN_NIUNIU:
        BaiRenNiuNiuDataMgr brnnDataMgr = ApplicationContext.getBean(BaiRenNiuNiuDataMgr.class);
        for (BaiRenNiuNiuRoom item : brnnDataMgr.newestRooms()) {
          BairenniuniuRoomDomain wrapper = item.getRoomDomain();
          if (wrapper.getIsActive() == RoomActiveConstant.ACTIVE) {
            rooms.add(RoomListDtoConvert.INSTANCE.toRoomListDto(item));
          }
        }

        break;
      default:
        break;
    }
    Result result = new Result(true);
    result.getMap().put("list", rooms);
    return result;
  }

  /**
   * 获取房间列表.
   * 
   * @return 房间列表.
   */
  @Path("/findAllRoomList")
  @POST
  public Result findAllRoomList() {

    List<RoomListDto> rooms = null;

    // 水浒传
    AllRoomListDto shuihu = new AllRoomListDto();
    shuihu.setGameId(GameIdConstant.SHUI_HU_ZHUAN);
    shuihu.setGameName(Game.getGame(GameIdConstant.SHUI_HU_ZHUAN).getDesc());

    rooms = new ArrayList<RoomListDto>();
    ShuiHuDataMgr shuiHuDataMgr = ApplicationContext.getBean(ShuiHuDataMgr.class);
    for (ShuiHuRoom item : shuiHuDataMgr.newestRooms()) {
      ShuihuRoomDomain roomDomain = item.getRoomDomain();
      if (roomDomain.getIsActive() == RoomActiveConstant.ACTIVE) {
        rooms.add(RoomListDtoConvert.INSTANCE.toRoomListDto(item));
      }
    }
    shuihu.setRooms(rooms);

    List<AllRoomListDto> list = new ArrayList<AllRoomListDto>();
    list.add(shuihu);

    // 水果拉霸
    AllRoomListDto laba = new AllRoomListDto();
    laba.setGameId(GameIdConstant.FRUIT_SLOT);
    laba.setGameName(Game.getGame(GameIdConstant.FRUIT_SLOT).getDesc());

    rooms = new ArrayList<RoomListDto>();
    FruitslotDataMgr labaDataMgr = ApplicationContext.getBean(FruitslotDataMgr.class);
    for (FruitslotRoom item : labaDataMgr.newestRooms()) {
      FruitslotRoomDomain roomDomain = item.getRoomDomain();
      if (roomDomain.getIsActive() == RoomActiveConstant.ACTIVE) {
        rooms.add(RoomListDtoConvert.INSTANCE.toRoomListDto(item));
      }
    }

    laba.setRooms(rooms);
    list.add(laba);

    // 飞禽走兽
    AllRoomListDto fqzs = new AllRoomListDto();
    fqzs.setGameId(GameIdConstant.FQZS);
    fqzs.setGameName(Game.getGame(GameIdConstant.FQZS).getDesc());

    rooms = new ArrayList<RoomListDto>();
    SharkDataMgr sharkDataMgr = ApplicationContext.getBean(SharkDataMgr.class);
    for (SharkRoom item : sharkDataMgr.newestRooms()) {
      SharkRoomDomain wrapper = item.getRoomDomain();
      if (wrapper.getIsActive() == RoomActiveConstant.ACTIVE) {
        rooms.add(RoomListDtoConvert.INSTANCE.toRoomListDto(item));
      }
    }
    fqzs.setRooms(rooms);
    list.add(fqzs);

    // 李逵捕鱼
    AllRoomListDto lkby = new AllRoomListDto();
    lkby.setGameId(GameIdConstant.LKBY);
    lkby.setGameName(Game.getGame(GameIdConstant.LKBY).getDesc());

    rooms = new ArrayList<RoomListDto>();
    LkbyDataMgr lkbyDataMgr = ApplicationContext.getBean(LkbyDataMgr.class);
    for (LkbyRoom item : lkbyDataMgr.newestRooms()) {
      LkpyRoomDomain roomDomain = item.getRoomDomain();
      if (roomDomain.getIsActive() == RoomActiveConstant.ACTIVE) {
        rooms.add(RoomListDtoConvert.INSTANCE.toRoomListDto(item));
      }
    }
    lkby.setRooms(rooms);
    list.add(lkby);

    // 金蝉捕鱼
    AllRoomListDto jcby = new AllRoomListDto();
    jcby.setGameId(GameIdConstant.JCBY);
    jcby.setGameName(Game.getGame(GameIdConstant.JCBY).getDesc());

    rooms = new ArrayList<RoomListDto>();
    JcbyRoomDic jcpyRoomDic = ApplicationContext.getBean(JcbyRoomDic.class);
    JcbyDataMgr jcbyDataMgr = ApplicationContext.getBean(JcbyDataMgr.class);
    for (JcbyRoom item : jcbyDataMgr.allRooms()) {
      JcbyRoomDomain roomDomain = jcpyRoomDic.get(item.getId().intValue());
      if (roomDomain.getIsActive() == RoomActiveConstant.ACTIVE) {
        rooms.add(RoomListDtoConvert.INSTANCE.toRoomListDto(item));
      }
    }
    jcby.setRooms(rooms);
    list.add(jcby);

    // 四人牛牛
    AllRoomListDto srnn = new AllRoomListDto();
    srnn.setGameId(GameIdConstant.SIREN_NIUNIU);
    srnn.setGameName(Game.getGame(GameIdConstant.SIREN_NIUNIU).getDesc());

    rooms = new ArrayList<RoomListDto>();
    SiRenNiuNiuDataMgr siRenNiuNiuDataMgr = ApplicationContext.getBean(SiRenNiuNiuDataMgr.class);
    for (SiRenNiuNiuRoom item : siRenNiuNiuDataMgr.newestRooms()) {
      if (item.getRoomDomain().getIsActive() == RoomActiveConstant.ACTIVE) {
        rooms.add(RoomListDtoConvert.INSTANCE.toRoomListDto(item));
      }
    }
    srnn.setRooms(rooms);
    list.add(srnn);

    // 炸金花
    AllRoomListDto zjh = new AllRoomListDto();
    zjh.setGameId(GameIdConstant.ZJH);
    zjh.setGameName(Game.getGame(GameIdConstant.ZJH).getDesc());

    rooms = new ArrayList<RoomListDto>();
    ZjhDataMgr zjhDataMgr = ApplicationContext.getBean(ZjhDataMgr.class);
    for (ZjhRoom item : zjhDataMgr.newestRooms()) {
      ZhajinhuaRoomDomWrapper wrapper = item.getRoomDomain();
      if (wrapper.getIsActive() == RoomActiveConstant.ACTIVE) {
        rooms.add(RoomListDtoConvert.INSTANCE.toRoomListDto(item));
      }
    }
    zjh.setRooms(rooms);
    list.add(zjh);

    // 黑杰克
    AllRoomListDto blackjack = new AllRoomListDto();
    blackjack.setGameId(GameIdConstant.BLACK_JACK);
    blackjack.setGameName(Game.getGame(GameIdConstant.BLACK_JACK).getDesc());

    rooms = new ArrayList<RoomListDto>();
    BlackjackDataMgr blackJDataMgr = ApplicationContext.getBean(BlackjackDataMgr.class);
    for (BlackjackRoom item : blackJDataMgr.newestRooms()) {
      BlackjackRoomDomain wrapper = item.getRoomDomain();
      if (wrapper.getIsActive() == RoomActiveConstant.ACTIVE) {
        rooms.add(RoomListDtoConvert.INSTANCE.toRoomListDto(item));
      }
    }
    blackjack.setRooms(rooms);
    list.add(blackjack);

    // 百家乐
    AllRoomListDto baccarat = new AllRoomListDto();
    baccarat.setGameId(GameIdConstant.BACCARAT);
    baccarat.setGameName(Game.getGame(GameIdConstant.BACCARAT).getDesc());


    rooms = new ArrayList<RoomListDto>();
    BaccaratDataMgr baccaratDataMgr = ApplicationContext.getBean(BaccaratDataMgr.class);

    for (BaccaratRoom item : baccaratDataMgr.newestRooms()) {
      BaccaratRoomDomain wrapper = item.getRoomDomain();
      if (wrapper.getIsActive() == RoomActiveConstant.ACTIVE) {
        rooms.add(RoomListDtoConvert.INSTANCE.toRoomListDto(item));
      }
    }
    baccarat.setRooms(rooms);
    list.add(baccarat);

    // 百人牛牛
    AllRoomListDto brnn = new AllRoomListDto();
    brnn.setGameId(GameIdConstant.BAIREN_NIUNIU);
    brnn.setGameName(Game.getGame(GameIdConstant.BAIREN_NIUNIU).getDesc());

    rooms = new ArrayList<RoomListDto>();
    BaiRenNiuNiuDataMgr baiRenNiuNiuDataMgr = ApplicationContext.getBean(BaiRenNiuNiuDataMgr.class);
    for (BaiRenNiuNiuRoom item : baiRenNiuNiuDataMgr.newestRooms()) {
      BairenniuniuRoomDomain roomDomain = item.getRoomDomain();
      if (roomDomain.getIsActive() == RoomActiveConstant.ACTIVE) {
        rooms.add(RoomListDtoConvert.INSTANCE.toRoomListDto(item));
      }
    }
    brnn.setRooms(rooms);
    list.add(brnn);

    Result result = new Result(true);
    result.getMap().put("list", list);
    return result;
  }

  /**
   * 获取游戏控制配置信息.
   * 
   * @Title findCtlCfgInfo. *
   * @author houdongsheng
   * @date 2018年1月27日 下午7:47:57
   * @param gameId 游戏编号
   * @return Result
   */
  @Path("/findCtlCfgInfo")
  @POST
  public Result findCtlCfgInfo(@NotNull @FormParam("gameId") Integer gameId) {
    Result result = new Result(true);
    switch (gameId) {
      case GameIdConstant.SHUI_HU_ZHUAN:
        List<ShuiHuControlDto> shuiHuControlDtoList = new ArrayList<ShuiHuControlDto>();
        for (ShuiHuDifficulty item : ShuiHuDifficulty.values()) {
          shuiHuControlDtoList.add(new ShuiHuControlDto(item.getDifficulty(), item.getName()));
        }
        result.getMap().put("difficultList", shuiHuControlDtoList);
        break;
      case GameIdConstant.FRUIT_SLOT:
        List<FruitSlotControlDto> fruitSlotControlDtoList = new ArrayList<FruitSlotControlDto>();
        for (FruitslotDifficulty item : FruitslotDifficulty.values()) {
          fruitSlotControlDtoList
              .add(new FruitSlotControlDto(item.getDifficulty(), item.getName()));
        }
        result.getMap().put("difficultList", fruitSlotControlDtoList);
        break;
      case GameIdConstant.FQZS:
        break;
      case GameIdConstant.LKBY: // 李逵捕鱼
        List<LkbyControlDto> lkbyControlDtoList = new ArrayList<LkbyControlDto>();
        for (LkbyDifficulty item : LkbyDifficulty.values()) {
          lkbyControlDtoList.add(new LkbyControlDto(item.getDifficulty(), item.getName()));
        }
        result.getMap().put("difficultList", lkbyControlDtoList);
        break;
      case GameIdConstant.JCBY: // 金蝉捕鱼
        List<JcbyControlDto> jcbyControlDtoList = new ArrayList<JcbyControlDto>();
        for (JcbyDifficulty item : JcbyDifficulty.values()) {
          jcbyControlDtoList.add(new JcbyControlDto(item.getDifficulty(), item.getName()));
        }
        result.getMap().put("difficultList", jcbyControlDtoList);
        break;
      case GameIdConstant.ZJH: // 炸金花
        break;
      case GameIdConstant.SIREN_NIUNIU: // 四人牛牛
        break;
      case GameIdConstant.BLACK_JACK: // 四人牛牛
        break;
      case GameIdConstant.BAIREN_NIUNIU: // 百人牛牛
        break;
      default:
        break;
    }
    return result;
  }
}
